package com.aaa.power.controller;

import com.aaa.common.constants.ExceptionConstant;
import com.aaa.common.constants.ReturnCode;
import com.aaa.common.util.CustomException;
import com.aaa.common.util.Result;
import com.aaa.power.entity.DictData;
import com.aaa.power.entity.DictType;
import com.aaa.power.service.DictTypeService;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * 字典类型表(DictType)表控制层
 *
 * @author makejava
 * @since 2020-11-16 18:58:07
 */
@RestController
@RequestMapping("dictType")
public class DictTypeController {
    /**
     * 服务对象
     */
    @Resource
    private DictTypeService dictTypeService;

    /**
     * 通过主键查询单条数据
     *
     * @param id 主键
     * @return 单条数据
     */
    @GetMapping("selectOne")
    public DictType selectOne(Integer id) {
        return this.dictTypeService.queryById(id);
    }

    /**
     * 新增
     */
    @PostMapping("insertDictType")
    public Result insertDictType(@RequestBody DictType dictType){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),dictTypeService.insert(dictType));
    }

    /**
     * 修改
     */
    @PutMapping("updateDictData")
    public Result updateDictData(@RequestBody DictType dictType){
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),dictTypeService.update(dictType));
    }

    /**
     * 删除
     */
    @DeleteMapping("deleteDictData")
    public Result deleteDictData(Integer dictId) {
        //当ID为空时抛出自定义异常
        if (null == dictId) {
            throw new CustomException(ExceptionConstant.ID_NOT_NULL.getErrorCode(),
                    ExceptionConstant.ID_NOT_NULL.getErrorMessage());
        }
        return new Result(ReturnCode.SUCCESSS.getCode(),
                ReturnCode.SUCCESSS.getMsg(),dictTypeService.deleteById(dictId));
    }

    /**
     * 查询所有
     *
     * @return
     */
    @GetMapping("queryAll")
    public Result queryAll() {
        return dictTypeService.queryAll();
    }

}